<template>
  <el-dialog
    title="地产信息"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="公司名称">
        <el-input
          v-model.trim="companyName"
          autocomplete="off"
          placeholder="请输入公司名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="公司介绍">
        <el-upload
          ref="imageUrl"
          :file-list="pixList1"
          action="imageUrl"
          :before-upload="imageUrlBeforeUpload"
          list-type="picture-card"
          :on-remove="removeFile"
          name="file"
          :limit="1"
          :http-request="uploadImage"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="介绍描述">
        <el-input
          v-model.trim="form.companyInfo"
          type="textarea"
          autocomplete="off"
          placeholder="请输入介绍描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="楼盘信息">
        <el-upload
          ref="imageUrl"
          :file-list="pixList2"
          action="imageUrl"
          :before-upload="imageUrlBeforeUpload"
          list-type="picture-card"
          :on-remove="removeFile1"
          name="file"
          :limit="1"
          :http-request="uploadImage1"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="楼盘描述">
        <el-input
          v-model.trim="form.estateDes"
          type="textarea"
          autocomplete="off"
          placeholder="请输入楼盘描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="公司动态">
        <el-upload
          ref="imageUrl"
          :file-list="pixList3"
          action="imageUrl"
          :before-upload="imageUrlBeforeUpload"
          list-type="picture-card"
          :on-remove="removeFile2"
          name="file"
          :limit="1"
          :http-request="uploadImage2"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="动态描述">
        <el-input
          v-model.trim="form.newsDes"
          type="textarea"
          autocomplete="off"
          placeholder="请输入动态描述"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { setCompanyInfo, getCompanyInfo } from '@/api/develop'
  import { showDeveloperInfo } from '@/api/user'
  import _net from '../../../config/net.config'

  import { upload } from '@/api/upload'
  export default {
    name: 'SetCompany',
    data() {
      return {
        companyName: '',
        companyId: '',
        form: {
          imgs: [],
          imgs1: [],
          imgs2: [],
          newsDes: '',
          estateDes: '',
          companyInfo: '',
        },
        dialogFormVisible: false,
        pixList1: [],
        pixList2: [],
        pixList3: [],
        row: {},
        isEdit: false,
      }
    },
    methods: {
      removeFile(file, fileList) {
        let _temp = this.pixList1.filter(function (item) {
          return item.id != file.id
        })
        this.pixList1 = _temp
      },
      removeFile1(file, fileList) {
        let _temp = this.pixList2.filter(function (item) {
          return item.id != file.id
        })
        this.pixList2 = _temp
      },
      removeFile2(file, fileList) {
        let _temp = this.pixList3.filter(function (item) {
          return item.id != file.id
        })
        this.pixList3 = _temp
      },
      imageUrlBeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
      async uploadImage(param) {
        console.log(param)
        const formData = new FormData()
        formData.append('fileType', 1)
        formData.append('file', param.file)
        console.log(formData)
        const { data } = await upload(formData)
        if (data.status == 0) {
          // let base = 'https://app.aixinfang.vip'
          let base = _net.baseURL
          const element = base + '/file/download?fileId=' + data.body.fileId
          this.pixList1.push({ url: element, id: data.body.fileId })
        } else {
          this.$baseMessage(data.message, 'error')
        }
      },
      async uploadImage1(param) {
        console.log(param)
        const formData = new FormData()
        formData.append('fileType', 1)
        formData.append('file', param.file)
        console.log(formData)
        const { data } = await upload(formData)
        if (data.status == 0) {
          // let base = 'https://app.aixinfang.vip'
          let base = _net.baseURL
          const element = base + '/file/download?fileId=' + data.body.fileId
          this.pixList2.push({ url: element, id: data.body.fileId })
        } else {
          this.$baseMessage(data.message, 'error')
        }
      },
      async uploadImage2(param) {
        console.log(param)
        const formData = new FormData()
        formData.append('fileType', 1)
        formData.append('file', param.file)
        console.log(formData)
        const { data } = await upload(formData)
        if (data.status == 0) {
          // let base = 'https://app.aixinfang.vip'
          let base = _net.baseURL
          const element = base + '/file/download?fileId=' + data.body.fileId
          this.pixList3.push({ url: element, id: data.body.fileId })
        } else {
          this.$baseMessage(data.message, 'error')
        }
      },
      showImage(pixString) {
        if (typeof pixString == undefined || pixString == null) {
          return null
        }
        // let base = 'https://note.zjut.club'
        let base = _net.baseURL
        let _file = []
        const element = base + '/file/download?fileId=' + pixString
        _file.push({ url: element, id: pixString })
        return _file
      },
      getImage(pixString) {
        if (typeof pixString == undefined || pixString == null) {
          return ''
        }
        let temp = []
        for (let index = 0; index < pixString.length; index++) {
          const element = pixString[index].id
          temp.push(element)
        }
        return temp.toString()
      },
      async showEdit(row) {
        this.resize()
        if (row.userId) {
          const { data } = await showDeveloperInfo(row.userId)
          if (data.status == 0) {
            console.log(data.body)
            if (data.body != null) {
              this.isEdit = true
              let result = data.body
              //获取现有的数据
              this.form = JSON.parse(result.companyDes)
              this.companyName = result.companyName
              this.companyId = result.companyId
              this.pixList1 = this.showImage(this.form.imgs)
              this.pixList2 = this.showImage(this.form.imgs1)
              this.pixList3 = this.showImage(this.form.imgs2)
            } else {
              this.isEdit = false
            }
          } else {
            this.$baseMessage(data.message, 'error')
          }
        }
        this.row = row

        this.dialogFormVisible = true
      },
      resize() {
        this.companyName = ''
        this.form = {
          imgs: [],
          imgs1: [],
          imgs2: [],
          newsDes: '',
          estateDes: '',
          companyInfo: '',
        }
        this.pixList1 = []
        this.pixList2 = []
        this.pixList3 = []
      },
      close() {
        this.resize()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.companyName == '') {
              this.$baseMessage('请填写公司名称', 'warning')
              return
            } else if (this.pixList1.length == 0) {
              this.$baseMessage('请上传公司图片', 'warning')
              return
            } else if (this.form.companyInfo == '') {
              this.$baseMessage('请填写公司描述', 'warning')
              return
            } else if (this.pixList2.length == 0) {
              this.$baseMessage('请上传楼盘图片', 'warning')
              return
            } else if (this.form.estateDes == '') {
              this.$baseMessage('请填写楼盘描述', 'warning')
            } else if (this.pixList3.length == 0) {
              this.$baseMessage('请上传资讯图片', 'warning')
              return
            } else if (this.form.newsDes == '') {
              this.$baseMessage('请填写资讯描述', 'warning')
              return
            }
            let _push_form = {}
            this.form.imgs = this.getImage(this.pixList1)
            this.form.imgs1 = this.getImage(this.pixList2)
            this.form.imgs2 = this.getImage(this.pixList3)
            _push_form.companyName = this.companyName
            _push_form.userId = this.row.userId
            if (this.isEdit) {
              _push_form.companyId = this.companyId
            }
            _push_form.companyDes = JSON.stringify(this.form)
            const { data } = await setCompanyInfo(_push_form)
            if (data.status == 0) {
              this.$baseMessage('设置地产信息成功！', 'success')
              this.$emit('fetch-data')
              this.close()
            } else {
              this.$baseMessage(data.message, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
